package main.java.com.web;

import main.java.com.service.DishService;
import main.java.com.service.impl.DishServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

//批量删除
@WebServlet("/BatchDeleteDishServlet")
public class BatchDeleteDishServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        String idsStr = request.getParameter("ids");
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong)
                .toList();

        DishService dishService = new DishServiceImpl();
        int deletedCount = dishService.batchDeleteDishes(ids);

        response.getWriter().write(String.valueOf(deletedCount));
    }
}
